using LifeChess.Entity;
using LifeChess.GamePlay.Test;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace LifeChess.GamePlay
{
    public class BoardCoordinateFlag : MonoBehaviour
    {
        public GameObject chessPrefab;

        public ChessLifeInfoPanel infoPanel;

        public Vector2Int coordinate;
        private Material material;

        public event Action<BaseChess> OnMouseClickEvent;

        private bool isMouseIn = false;
        private bool isMouseInDown = false;
        private bool isChessOn = false;

        private void Awake()
        {
            material = transform.GetChild(0).GetComponent<MeshRenderer>().material;
        }

        private void OnMouseEnter()
        {
            isMouseIn = true;
            if (!isChessOn)
                material.color = Color.grey;
            else
                material.color = Color.black;
        }

        private void OnMouseExit()
        {
            isMouseIn = false;
            if (!isChessOn)
                material.color = Color.white;
            else
                material.color = Color.red;
        }

        private void OnMouseDown()
        {
            isMouseInDown = true;
            isMouseIn = true;
        }

        private void OnMouseUp()
        {
            if (isMouseIn && isMouseInDown && !isChessOn)
            {
                // Set chess flag
                isChessOn = true;

                // instance chess object
                GameObject instanceObject = GameObject.Instantiate(chessPrefab);
                Vector3 newPos = instanceObject.transform.position;
                newPos.x = transform.position.x;
                newPos.y = transform.position.y;
                instanceObject.transform.position = newPos;
                instanceObject.transform.parent = this.gameObject.transform;
                // Instance chess data
                BaseChess chess = new BaseChess();
                chess.InitChess(12, ChessConst.currentChessTeam);
                // Synchronize the data
                OnMouseClickEvent?.Invoke(chess);
                // Set chess for infomation script
                ChessInfoSeek chessInfoSeek = instanceObject.GetComponent<ChessInfoSeek>();
                chessInfoSeek.curChess = chess;
                chessInfoSeek.infoPanel = infoPanel;
                // Set team color
                Material chessMaterial = instanceObject.transform.GetChild(0).GetComponent<MeshRenderer>().material;
                if (chess.Team == ChessTeam.Player2)
                {
                    chessMaterial.color = Color.black;
                }
                // Registe the event when chess dead
                chess.OnChessDead += () =>
                {
                    Destroy(instanceObject);
                    isChessOn = false;
                    if (isMouseIn)
                    {
                        material.color = Color.grey;
                    }
                    else
                    {
                        material.color = Color.white;
                    }
                };
                // Change the color of the unit of the board
                material.color = Color.black;
            }
        }
    }
}
